<template>
  <el-container class="main_container">
    <div class="solver-shake">
      <div class="player">
        <Player></Player>
      </div>
    </div>
    <!-- 头部布局 -->
    <el-header>
      <!-- logo和项目名称 -->
      <div class="left_box">
        <span>{{$store.state.user.type == 1?'学生界面':'教师界面'}}</span>
      </div>
      <!-- 用户登录展示的头像 -->
      <div class="right_box">
        <el-dropdown>
          <!-- 头衔缩略图 -->
          <pan-thumb :width="'50px'" :height="'50px'" :image="user.avatar" />
          <!-- 下拉菜单 -->
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="logout" icon="el-icon-switch-button">退出登录</el-dropdown-item>
            <el-dropdown-item @click.native="modified" icon="el-icon-switch-button">修改个人资料</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 展开/收起 -->
        <div class="toggle_box" @click="toggleCollapse">
          <i class="el-icon-s-operation"></i>
        </div>
        <!-- 菜单容器 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333333"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="isCollapse"
          :collapse-transition="false"
          :unique-opened="true"
          router
        >
          <!-- 菜单内容 -->
          <MenuTree :menuList="this.menuList"></MenuTree>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <el-card class="box-card">
          <!-- 路由视图 -->
          <router-view></router-view>
        </el-card>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 导入组件
import MenuTree from "../components/MenuTree";
import PanThumb from "../components/avatar-upload/PanThumb";
import Player from "../components/vue_musicplayer/Player";
import { getMenu, getMenuForCourse } from "../api/menu";

export default {
  name: "Main",
  data() {
    return {
      // 弹窗控制键
      addOrUpdateVisible: false,
      isCollapse: false,
      activePath: "",
      openPlayer: false,
      musicVisible: false,
      menuList: [],
      user: {
        type: "",
        id: "",
        avatar:
          "https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191"
      }
    };
  },
  //  注册组件
  components: {
    MenuTree,
    PanThumb,
    Player
  },
  created() {
    this.getMenu();
    this.user = this.$store.state.user;
  },
  mounted() {
    this.$router.push({
      path: "/list"
    });
  },
  computed: {},
  methods: {
    player() {
      this.musicVisible = true;
    },
    // 监听子组件弹窗关闭后触发，有子组件调用
    showAddOrUpdate(data) {
      if (data === "false") {
        this.addOrUpdateVisible = false;
      } else {
        this.addOrUpdateVisible = true;
      }
    },
    logout() {
      this.$store.commit("logout");
      window.sessionStorage.removeItem("user");
      window.sessionStorage.clear();
      this.$router.push("/main");
      location.reload();
    },
    modified() {
      let user = JSON.stringify(this.user);
      this.$router.push({
        path: "/user/changeAvatar",
        query: { user }
      });
    },
    // 显示弹框
    show() {
      this.addOrUpdateVisible = true;
    },
    // 控制左侧栏开关
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 从后台接受主菜单数据
    async getMenu() {
      const { data } = await getMenu();
      this.menuList = data.data.data;
    },
    // 从后台接受课程菜单数据
    async getMenuForCourse() {
      const { data } = await getMenuForCourse();
      this.menuList = data.data.data;
    }
  },
  watch: {
    $route: {
      handler: function(to, from) {
        let toName = to.name;
        if (toName == "CourseDetail") {
          this.getMenuForCourse();
        } else {
          this.getMenu();
        }
      },
      deep: true
    }
  }
};
</script>

<style lang="scss" scpoed>
.main_container {
  height: 100%;
}
/* 下拉菜单的样式 */
.el-dropdown-link {
  cursor: pointer;
  color: #2c2d2e;
}
.el-icon-arrow-down {
  font-size: 12px;
}
// 头部样式
.el-header {
  background-color: #333333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  padding-left: 0;
  color: #ffffff;
  // 左边logo和标题
  .left_box {
    display: flex;
    align-items: center;

    // logo
    img {
      width: 60px;
      height: 60px;
      margin: 0px 0px 10px 15px;
    }

    // 标题
    span {
      margin-left: 15px;
    }
  }
  // 右边登陆头像
  .right_box {
    margin-right: 25px;
    .el-dropdown {
      & > img {
        height: 60px;
        width: 60px;
        border-radius: 50%;
        background-color: #ffffff;
        margin: 0 15px 0 0;
      }
    }
  }
}
// 侧边栏
.el-aside {
  background-color: #333333;
  color: #fff;
  transition: width 0.4s;
  position: relative;
  z-index: 1;
  .el-menu {
    border-right: none;
  }
  // 展开/收起
  .toggle_box {
    background-color: #333333;
    border: 1px solid white;
    border-right: 0;
    font-size: 20px;
    font-weight: bold;
    line-height: 25px;
    color: #cccc00;
    letter-spacing: 0.2em;
    text-align: center;
    cursor: pointer;
  }
}
// 主要内容
.el-main {
  background-color: #e9eef3;
}
.box-card {
  min-height: 800px;
  max-height: 800px;
  overflow: scroll;
}
#player {
  width: 100%;
  height: 100%;
}
.player {
  position: absolute;
  bottom: 0;
  z-index: 2;
}
.solver-shake{
  min-width: 478px;
  min-height: 902px;
  max-height: 902px;
  position: absolute;
}
</style>

